<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class msg_m extends CI_Model{
	
	public function __construct()
	{
		parent::__construct();	
	}
	
	
	/**
	 * 获取消息表
	 * @param array $data
	 */
	public function getMsg($data,$off = null,$limit = null)
	{
		$sql	   = '';
		$sql_order = '';
		$sql_limit = "";
		$implode = array();
		
		if (isset($data['accountID']) && !empty($data['accountID'])) {
			$implode[] = "accountID='" . intval($data['accountID']) . "'";
		}
		
		if (isset($data['id']) && !empty($data['id'])) {
			$implode[] = "id=" . intval($data['id']);
		}
		
		if (isset($data['status'])) {
			$implode[] = "status='" . $data['status'] . "'";
		}
		
		if (isset($data['noStatus']) && !empty($data['noStatus'])) {
			$implode[] = "status !=" . $data['noStatus'];
		}
		
		if (isset($data['sort']) && isset($data['order']))
		{
			$order = 'ASC';
			if ($data['order'] == 'DESC')
			{
				$order = 'DESC';
			}
			
			if ($data['sort'] == 'startTime')
			{
				$sql_order .= ' ORDER BY startTime '.$order;
			}
		}
		
		if ($implode) {
			$sql = "WHERE " . implode("AND ", $implode);
		}
		
		if ($off != null && $limit != null){
			$sql_limit = "Limit ".$off.",".$limit;
		}
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "msg " . $sql.$sql_order.$sql_limit);

		return $query->result_array();
	}
	
	/** 修改消息表
	 * @param int $id
	 * @param array $data
	 * @return boolean
	 */
	public function setMsg($id,$data)
	{
		$toSet   = array();
		
		$toWhere = array(
				'id'  => $id,
				);
		
		if (isset($data['title']) && !empty($data['title'])) {
			$toSet['title'] = $data['title'];
		}
		
		if (isset($data['content']) && !empty($data['content'])) {
			$toSet['content'] = $data['content'];
		}
	
		
		if (isset($data['status']) && !empty($data['status'])) {
			$toSet['status'] = $data['status'];
		}
		
		if (empty($toSet))
		{
			return false;
		}
		
		$sql = $this->db->update_string(DB_PREFIX.'msg',$toSet,$toWhere);
		
		return $this->db->query($sql);
	}
	
	/**
	 * 添加消息表信息
	 * @param array $data
	 * @return boolean
	 */
	public function addMsg($data)
	{
		$sql	 = '';
		$implode = array();
		
		if (isset($data['accountID']) && !empty($data['accountID'])) {
			$implode[] = "accountID='" . $data['accountID'] . "'";
		}
		
		if (isset($data['title']) && !empty($data['title'])) {
			$implode[] = "title='" . $data['title'] . "'";
		}
		
		if (isset($data['content']) && !is_null($data['content'])) {
			$implode[] = "content='" . $data['content'] . "'";
		}
		
		if (isset($data['total']) && !is_null($data['total'])) {
			$implode[] = "total='" . $data['total'] . "'";
		}
		

		if (isset($data['startTime']) && !is_null($data['startTime'])) {
			$implode[] = "startTime='" . $data['startTime'] . "'";
		}
		else
		{
			$implode[] = "startTime='" . date('Y-m-d H:i:s') . "'";
		}

		if (isset($data['status']) && !empty($data['status'])) {
			$implode[] = "status='" . (int)$data['status'] . "'";
		}
		
		if ($implode) {
			$sql = " " . implode(", ", $implode);
		}
		else {
			return false;
		}
		
		$this->db->query("INSERT " . DB_PREFIX . "msg SET " . $sql);
		
		return $this->db->insert_id();
	}
	
	
	public function getMsgList($data)
	{
		$sql	   = '';
		$implode = array();
	
		if (isset($data['msgID']) && !empty($data['msgID'])) {
			$implode[] = "msgID='" . intval($data['msgID']) . "'";
		}
	
		if (isset($data['id']) && !empty($data['id'])) {
			$implode[] = "id='" . intval($data['id']) . "'";
		}
	
		if (isset($data['status']) && !empty($data['status'])) {
			$implode[] = "status='" . $data['status'] . "'";
		}
	
		if ($implode) {
			$sql = "WHERE " . implode("AND ", $implode);
		}
	
		echo "SELECT * FROM " . DB_PREFIX . "msg_list " . $sql;
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "msg_list " . $sql);
	
		return $query->result_array();
	}
	
	
	/** 添加消息列表信息
	 * @param array $data
	 * @return boolean
	 */
	public function addMsgList($data)
	{
		$sql	 = '';
		$implode = array();
	
		if (isset($data['msgID']) && !empty($data['msgID'])) {
			$implode[] = "msgID='" . $data['msgID'] . "'";
		}
	
		if (isset($data['targetID']) && !empty($data['targetID'])) {
			$implode[] = "targetID='" . $data['targetID'] . "'";
		}
		
		if (isset($data['status']) && !empty($data['status'])) {
			$implode[] = "status='" . $data['status'] . "'";
		}
	
		if ($implode) {
			$sql = " " . implode(", ", $implode);
		}
		else {
			return false;
		}
	
		$this->db->query("INSERT " . DB_PREFIX . "msg_list SET " . $sql);
	
		return $this->db->insert_id();
	}
	
	
	/**
	 * 修改消息表信息
	 * @param int $id
	 * @param array $data
	 * @return boolean
	 */
	public function setMsgList($id,$data)
	{
		$toSet   = array();
	
		$toWhere = array(
				'id'  => $id,
		);
	
		if (isset($data['status']) && !empty($data['status'])) {
			$toSet['status'] = $data['status'];
		}
	
		if (empty($toSet))
		{
			return false;
		}
	
		$sql = $this->db->update_string(DB_PREFIX.'msg_list',$toSet,$toWhere);
	
		return $this->db->query($sql);
	}
	
}